<?php

!defined('DEBUG') AND exit('Access Denied.');
/**
 * 帖子扩展设置
 * admin/plugin-setting-ghx_threadext.htm
 */
$action = param(3);
empty($action) AND $action = 'set';
if(!isset($dir)) $dir = param(2);

if ($action == 'set') {
    $header['title'] = lang('threadext_set').','.$header['title'];

    $UserLicenseKey = GetPluginCfgNode('UserLicenseKey');
    $threadext_doc_url = 'http://soft.ahfrc.com/xiunoplugin/threadext_doct.html?uk=' . $UserLicenseKey;
    $full_path = $ghx_cache->get_share_var(VAR_APPPATH).'plugin'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.'conf.json';
    $threadext_tab_active = admin_tab_active(array(
        'set'=>array('url'=>url('plugin-setting-'.$dir.'-set'), 'text'=>lang('threadext_set')),
        'agreement'=>array('url'=>url('plugin-setting-'.$dir.'-agreement'), 'text'=>lang('user_agreement'))
    ), $action);

    if ($method == 'GET') {
        $threadext_cfg = '';
        $get_cfg_succ = false;
        if (is_file($full_path)) {
            $threadext_cfg = file_get_contents($full_path);
            $threadext_c2 = '';
            if (!empty($threadext_cfg)) {
                $delhp = stripos($threadext_cfg, '"UserLicenseKey"');
                if ($delhp > 0) {
                    while ($delhp > 1) {
                        $delhp--;
                        if ($threadext_cfg{$delhp} == ',') break;
                    }
                    if ($delhp > 0) {
                        $delhp0 = strpos($threadext_cfg, '{');
                        if ($delhp0 !== false) {
                            $threadext_c2 = substr($threadext_cfg, 0, $delhp0 + 1) . substr($threadext_cfg, $delhp + 1);
                        }
                    }
                }
            }
            if ($threadext_c2 == '') {
                $threadext_cfg = '配置被恶意篡改,读取失败!' . PHP_EOL . 'Configuring malicious tampering, failure to read!';
            } else {
                $threadext_cfg = &$threadext_c2;
                $get_cfg_succ = true;
            }
        }
        include _include(APP_PATH . 'plugin/' . $dir . '/htm/setting.htm');

    } else {
        $threadext_c2 = param('threadextset', '', false);
        $syscfg = '';
        $msg = '';
        $retcode = -1;
        if (!empty($threadext_c2)) {
            if ("\r\n" != PHP_EOL)
                $threadext_c2 = str_replace("\r\n", PHP_EOL, $threadext_c2);
            $p0 = strpos($threadext_c2, '{');
            $p1 = strpos($threadext_c2, '}');
            if ($p0 !== false && $p1 > $p0) {
                if (is_file($full_path)) {
                    $threadext_cfg = file_get_contents($full_path);
                    if (!empty($threadext_cfg)) {
                        $delhp = stripos($threadext_cfg, '"UserLicenseKey"');
                        if ($delhp > 0) {
                            while ($delhp > 1) {
                                $delhp--;
                                if ($threadext_cfg{$delhp} == ',') break;
                            }
                        }
                        if ($delhp > 0) {
                            $syscfg = substr($threadext_cfg, $p0 + 1, $delhp - $p0);
                        }
                    }
                }
            }
            if (!empty($syscfg)) {
                $threadext_cfgX = substr($threadext_c2, 0, $p0 + 1) . $syscfg . substr($threadext_c2, $p0 + 1);
                if ($threadext_cfgX != $threadext_cfg) {
                    $testv = xn_json_decode($threadext_cfgX);
                    if(!empty($testv) && is_array($testv) && isset($testv['UserLicenseKey'])) {
                        if (file_put_contents($full_path, $threadext_cfgX, LOCK_EX)) {
                            $msg = '修改成功';
                            $retcode = 0;
                            @clearstatcache(true, $full_path);
                        } else {
                            $msg = '保存失败';
                            $retcode = -2;
                        }
                    }
                    else{
                        $msg = '格式错误';
                        $retcode = -4;
                    }
                } else {
                    $retcode = -3;
                    $msg = '内容没有改变!';
                }
            }
        }
        message($retcode, $msg);
    }
}
elseif($action == 'agreement') {
    $header['title'] = lang('user_agreement').','.$header['title'];
    include _include(APP_PATH.'plugin/'.$dir.'/htm/agreement.htm');

}